﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>选择接受部门</title>
    <!--
    1, 退回界面。
    2，需要 FK_Flow, FK_Node,WorkID, FID.
    3, 调用方法 ReturnWork.htm?FK_Flow=001&FK_Node=103&WorkID=232&FID=23
    -->
    
    <link href="../../../Scripts/bootstrap/css/bootstrap.css" rel="stylesheet" />
    <link href="../../../Scripts/bootstrap/css/components-rounded.css" rel="stylesheet" />
    <link href="../../../Scripts/bootstrap/css/font-awesome.css" rel="stylesheet" />
    <link href="../../../Scripts/bootstrap/css/themes/default/style.min.css" rel="stylesheet" />
    <link href="../../../../DataUser/Style/ccbpm.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="../../../Scripts/bootstrap/js/jquery.min.js"></script>
    <script type="text/javascript" src="../../../Scripts/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="../../../Scripts/commonYangYH.js"></script>
    <script src="../../../Scripts/QueryString.js" type="text/javascript"></script>
    <script type="text/javascript" src="../../../Scripts/bootstrap/js/jquery.cokie.min.js"></script>
    <script type="text/javascript" src="../../../Scripts/bootstrap/js/jstree.js"></script>
    <script type="text/javascript" src="../../../Scripts/bootstrap/js/jsTree.ddl.js"></script>
    <!-- 导入配置文件. -->
    <script type="text/javascript" src="../../../Scripts/config.js"></script>
    <script type="text/javascript" src="../../../Comm/Gener.js"></script>
    <link href="../../../Scripts/layui/layui/css/layui.css" rel="stylesheet" />
    <style type="text/css">
        #TB_Doc {
            height: 132px;
            width: 433px;
        }
    </style>
    <script type="text/javascript">
        var webUser = new WebUser();
        $(function () {

            InitTreeData();
            bindTest();

        });

        //确定 人员选择
        function SelectEmps() {

            GetSelectedEmpsReturnVal();

            //No  是返回到别的页面的值
            if (returnVal.No == undefined || returnVal.No == '') {
                alert("未选择部门");
                return;
            }

            var en = new Entity("BP.CCFast.CCMenu.PowerCenter");
            en.CopyURL();
            en.CtrlModel = "Depts";

            en.IDs = returnVal.No;
            en.IDNames = returnVal.Name;
            en.Insert();

            var url = "../PowerCenter.htm";
            url += "?CtrlObj=" + GetQueryString("CtrlObj") + "&CtrlPKVal=" + GetQueryString("CtrlPKVal") + "&CtrlGroup=" + GetQueryString("CtrlGroup");
            SetHref(url);

            window.parent.selectEmpsWindowClose("确定");

        }
        var webUser = new WebUser();
        var pageData = { IsSelectMore: true, FK_Dept: webUser.FK_Dept || '', ParentDept: '' };
        var returnVal = {};
        var isFirst = true;

        //初始化树  接收人
        function InitTreeData() {
            $('#OrgJSTree').css('display', 'block');

            var url = '';

            //初始化Tree
            var plugins = [];
            if (pageData.IsSelectMore == true) { //多选
                plugins = [
                    "checkbox",
                    "state",
                    "conditionalselect"
                ];
            }
            //返回节点数组
            var initNodeData = function (data, fk_dept2) {

                //data = cceval('(' + data + ')');

                var fk_dept = fk_dept2; //pageData.FK_Dept;
                var parentNo = undefined;
                var fkDeptObj = $.grep(data, function (value) {
                    return value.No == fk_dept;
                });
                if (fkDeptObj != undefined && fkDeptObj.length == 1) {
                    parentNo = fkDeptObj[0].ParentNo;
                }
                pageData.FK_Dept = parentNo;
                var instance = $('#OrgJSTree').jstree(true);
                var depts = $.grep(data, function (value) {
                    return value.ParentNo == parentNo;
                })

                //子部门
                var childDepts = $.grep(data, function (value) {
                    return value.ParentNo == fk_dept;
                })
              
                var itemArr = [];
                $.each(depts, function (i, dept) {
                    var deptNode = {
                        "text": dept.Name,
                        "id": dept.No,
                        "state": { "opened": true },
                        "children": true,
                        "data": dept
                    };

                    if (dept.No == fk_dept) {  //是本部门时加载子部门和人员
                        var children = [];
                        if (childDepts.length > 0) {
                            $.each(childDepts, function (i, childDept) {
                                var childDeptNode = {
                                    "text": childDept.Name,
                                    "id": childDept.No,
                                    "children": true,
                                    "data": childDept
                                };

                                children.push(childDeptNode);
                            });
                        }
                       
                        deptNode.data.childrenData = children;
                    }

                    itemArr.push(deptNode);
                });

                if (instance == false || instance.get_node(fk_dept) == false) {
                    pageData.ParentDept = parentNo;
                }
                else {
                    if (itemArr.length>0)
                    itemArr = itemArr[0].data.childrenData;
                }
                return itemArr;

            };

            $('#OrgJSTree').jstree({
                'core': {

                    'data': function (node, cb) {

                        //初始化根节点
                        if (node.id == "#") {

                            var handler = new HttpHandler("BP.WF.HttpHandler.GPMPage");
                            if (webUser.CCBPMRunModel == 2)
                                handler.AddPara("ParentNo", webUser.OrgNo);
                            else
                                handler.AddPara("ParentNo", pageData.FK_Dept);
                            var data = handler.DoMethodReturnString("Organization_Init");
                            if (data.indexOf('err@') == 0) {
                                alert(data);
                                return;
                            }
                            data = JSON.parse(data);
                            var depts = $.grep(data, function (item) {
                                return item.No == pageData.FK_Dept;
                            });
                            if (depts.length == 0) {
                                var dept = new Entity("BP.Port.Dept", pageData.FK_Dept)
                                data.push({
                                    No: dept.No,
                                    Name: dept.Name,
                                    ParentNo: dept.ParentNo
                                })
                            }
                            var fk_dept2 = pageData.FK_Dept;
                            cb(initNodeData(data, fk_dept2));


                        }
                        else {
                            if (node.data != undefined && node.data.childrenData != undefined && node.data.childrenData.length > 0) {
                                var fk_dept2 = node.id;
                                cb(node.data.childrenData, fk_dept2);
                            }
                            else {
                                pageData.FK_Dept = node.id;

                                var handler = new HttpHandler("BP.WF.HttpHandler.GPMPage");
                                handler.AddPara("ParentNo", node.id);
                                var data = handler.DoMethodReturnString("Organization_Init");

                                if (data.indexOf('err@') == 0) {
                                    alert(data);
                                    return;
                                }
                                data = JSON.parse(data);
                                var depts = $.grep(data, function (item) {
                                    return item.No == pageData.FK_Dept;
                                });
                                if (depts.length == 0) {
                                    var dept = new Entity("BP.Port.Dept", pageData.FK_Dept)
                                    data.push({
                                        No: dept.No,
                                        Name: dept.Name,
                                        ParentNo: dept.ParentNo
                                    })
                                }
                                var fk_dept2 = node.id;
                                cb(initNodeData(data, fk_dept2));
                            }
                        }
                    }
                },
                "plugins": plugins,
                "checkbox": {
                    "keep_selected_style": false,//是否默认选中
                    "three_state": true,//父子级别级联选择
                    "tie_selection": true
                },
            }).on('changed.jstree', function (node, cb) {
                GetSelectedEmpsReturnVal();
            }).bind("loaded.jstree", function (event, data) {
                data.instance.open_node();

            }).bind("select_node.jstree", function (e, data) { //点击事件
                //触发toggle_node 事件 就行了
                if (isFirst == true)
                    isFirst = false;
                else
                    $('#OrgJSTree').jstree("toggle_node", data.node);

            }).on("loaded.jstree", function (event, data) {
                if (typeof data.instance.clear_state === "function")
                    data.instance.clear_state(); // <<< 这句清除jstree保存的选中状态
            });
        }
        //弹出树
        function bindTest() {
            $('#test').bind('click', function () {
                if ($('#OrgJSTree').css('display') == "none") {
                    $('#OrgJSTree').css('display', 'block');
                    var instance = $('#OrgJSTree').jstree(true);
                    if ($('#orgTxt').val() != undefined && $('#orgTxt').val() != '') {
                        instance.select_node($('#orgTxt').val());
                    }
                }
                else {
                    $('#OrgJSTree').css('display', 'none');
                }
            });

            $('#toPrevDept').bind('click', function () {
                if (pageData.ParentDept == 'null' || pageData.ParentDept == 0) {
                    alert("已到第一级机构");
                    return;
                }
                var webUser = new WebUser();
                // SAAS模式下，限制只可以看到OrgNo级别
                if (webUser.CCBPMRunModel != 0 && pageData.ParentDept == 100) {
                    alert("已到第一级机构");
                    return;
                }


                pageData.FK_Dept = pageData.ParentDept;

                $('#OrgJSTree').jstree(true).refresh();
            });
        }
        //获取树选中的值
        function GetSelectedEmpsReturnVal() {

            var instance = $('#OrgJSTree').jstree(true);

            var selectedIdArr = instance.get_selected();
            var selectedNodes = [];

            $.each(selectedIdArr, function (i, selectedId) {
                selectedNodes.push(instance.get_node(selectedId).data);
            });

            if (selectedNodes.length > 0) {
                for (var property in selectedNodes[0]) {
                    returnVal[property] = [];
                }
            }
            $.each(selectedNodes, function (i, selected) {
                for (var property in returnVal) {
                    returnVal[property].push(selected[property]);
                }
            });
            for (var property in selectedNodes[0]) {
                returnVal[property] = returnVal[property].join(",");
            }

        }

        function Search() {
            var keyVal = $("#TB_Key").val();
            if (keyVal == "")
                return;
            //根据输入的部门编号或者名称获取部门信息
            var ens = new Entities("BP.Port.Depts");
            ens.RetrieveCond("No,Name", "like", '%' + keyVal + '%');

            //查询到的部门节点就展开
            var $jstree = $('#OrgJSTree').jstree(true);
            for (var i = 0; i < ens.length; i++) {
                $jstree.open_node($jstree.get_node(ens[i].No));

            }

        }
        function Close() {
            var url = "../PowerCenter.htm";
            url += "?CtrlObj=" + GetQueryString("CtrlObj") + "&CtrlPKVal=" + GetQueryString("CtrlPKVal") + "&CtrlGroup=" + GetQueryString("CtrlGroup");
            SetHref(url);

            window.parent.selectEmpsWindowClose("确定");
        }
    </script>
</head>
<body>
    <div style="background:#efefef; padding:5px 10px;">
        <button type="button" id="toPrevDept" class="layui-btn layui-btn-sm">上一级</button>
        <button type="button" onclick="Close()" class="layui-btn layui-btn-sm layui-btn-primary">取消</button>
        <button type="button" onclick="SelectEmps()" class="layui-btn layui-btn-sm layui-btn-normal">确定</button>
    </div>
    <div id="OrgJSTree" class="tree-demo jstree jstree-1 jstree-default" style="position:absolute;left:0;top:45px; overflow-y:auto;"></div>
</body>
</html>
